/**
 * @author gzbugu
 */

//发布信息验证 

$(function(){
	
	var errorinfo = $('#errorinfo').val();
	if('picType' == errorinfo){
		alert('仅支持上传以下格式的图片:jpg/gif/png');
	}
	
	loadCategory();
	
	$('#saveNews').click(function(){
		var oEditor = FCKeditorAPI.GetInstance("news.content");   
		var htmlContent = oEditor.GetXHTML()
		var content = $.trim(htmlContent.replace("<br />","")); 
		var title=$.trim($('#title').val());
		
		//校验标题
		if(title.length < 1){
			alert("请填写文章标题");
			$("#title").focus();
			return false;
		}else if(content.length < 1){
			alert("请填写文章内容");
			return false;
		}else{
			
			if($("#readTimes").val() == ""){
				$("#readTimes").val(0);
			}
			
			$("#newsform").submit();
		}
	});
	
	$('#updateNews').click(function(){
		var oEditor = FCKeditorAPI.GetInstance("news.content");   
		var htmlContent = oEditor.GetXHTML()
		var content = $.trim(htmlContent.replace("<br />","")); 
		var title=$.trim($('#title').val());
		
		//校验标题
		if(title.length < 1){
			alert("请填写文章标题");
			$("#title").focus();
			return false;
		}else if(content.length < 1){
			alert("请填写文章标题");
			return false;
		}else{
			
			if($("#readTimes").val() == ""){
				$("#readTimes").val(0);
			}
			
			$("#newsform").submit();
		}
	});
	
	
	$('#returnNews').click(function(){
		window.history.go(-1);
	});
	
	$('a.deleteFile').live("click",function(){
		var id = $(this).attr("id");
		if(confirm("确定要删除该图片吗?")){
			$.ajax({
				url:'deleteNewsFile.action',
				type: 'post',
				data:{fileId:id},
				cache: false,
				success: function(data){
					if("success" == data){
						$('#picDiv').hide();
						$("#pic").show();
					}else if("login" == data){
						top.location.href = "/index.jsp";
					}else{
						alert("网络异常,请稍后重试");
					}
				}
			})
		}
	});
	
})

function loadCategory(){
	var ncId = $("#catagoryId").val();
	
	$.ajax({
		url:'loadCategory.action',
		type: 'post',
		cache: false,
		dataType: 'json',
		success: function(data){
			data = eval(data);
			$.each(data, function(index, item){
		       var node = '<option value="';
			   node += item.id;
			   node += '">';
			   for(i=0;i<item.levels-1;i++){
			   		node += '&nbsp;&nbsp;';
			   }
			   node += item.nameCn;
			   node += '</option>';
			   $("#newsCategory").append(node);
		    });
			//var nodeFaqs = '<option value="faqs">Faqs</option>';
			//var nodeJob = '<option value="job">Job Opportunitines</option>';
			// $("#newsCategory").append(nodeFaqs);
			//  $("#newsCategory").append(nodeJob);
			setTimeout(function(){ $("#newsCategory").val(ncId)},200);
		}
	});
}




